home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2781 < prev    next >
Encoding:
Text File  |  1996-08-06  |  4.3 KB  |  111 lines

  1. Path: engnews2.Eng.Sun.COM!usenet
  2. From: nitin@more.eng.sun.com (Nitin More [CONTRACTOR])
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: [Q] Dynamically allocating memory for a char*
  5. Date: 19 Jan 1996 17:50:12 GMT
  6. Organization: SunSoft
  7. Message-ID: <NITIN.96Jan19095012@more.eng.sun.com>
  8. References: <4dmn1i$10t@walrus2.walrus.com>
  9. NNTP-Posting-Host: more.eng.sun.com
  10. In-reply-to: fjordao@walrus.com's message of Fri, 19 Jan 1996 00:00:49 GMT
  11.  
  12.  
  13. To read a string from cin, you need a place to store it (char *) with enough
  14. memory allocated.  Until you read the string, you won't know what is enough.
  15. This is a catch-22.  The best you can do is use a temporary variable with
  16. (reasonable) maximum possible size, read the string in the temporary variable,
  17. get the size plus one for the NULL character, allocate that much memory
  18. pointed to by your original variable and copy the string into it from the
  19. temporary variable.
  20.  
  21. Since you may get into situation where the input string is bigger than the
  22. maximum size you have chosen, you could do the following.  Set the last char
  23. in the temporary variable to be NULL (i.e. 0) and check if it is still the
  24. same after reading the string.  If it is same then you are safe.  If not, the
  25. string read is bigger than your maximum size and you don't have the complete
  26. string.  [Worse yet, you might have overwritten memory of some other
  27. variable].  Either you can display an error and quit or reallocate bigger size
  28. (e.g., 2*maximum) for the temporary and try again.  Try until you succeed.
  29.  
  30. Anybody else knows a better way?
  31.  
  32. -Nitin
  33.  
  34.  
  35. In article <4dmn1i$10t@walrus2.walrus.com> fjordao@walrus.com (Felipe Jordao) writes:
  36.  
  37. > From: fjordao@walrus.com (Felipe Jordao)
  38. > Newsgroups: comp.lang.c++
  39. > Date: Fri, 19 Jan 1996 00:00:49 GMT
  40. > Organization: HAC
  41. > Hi,
  42. > I have a question about allocating memory for a string as it's passed
  43. > in by the user.  I'm using cin to get the info, but maybe this is
  44. > inappropriate.  I have included the relevant snippets of the file
  45. > below.  What I am trying to achieve is no limitation for the user when
  46. > they input a path + filename.  Is it possible to use char* foo and
  47. > then something like (I know this is wrong, but the idea of it ...)
  48. > cin >> malloc(sizeof(infile));             :0
  49. > Here is the actual code
  50. > ----------------------------------------------------------------------
  51. > main()
  52. > {
  53. >  ifstream InputFile;     // Input file stream
  54. >  ofstream OutputFile;    // Output file stream
  55. >  .
  56. >  .
  57. >  .
  58. >  OpenFiles(InputFile, OutputFile);
  59. >  .
  60. >  .
  61. > }
  62. > void OpenFiles(ifstream& InputFile, ofstream& OutputFile)
  63. > {
  64. >  char infile[30];        // <---- RIGHT HERE.  this works as it is, 
  65. >  char outfile[30];       //       but I would like to use
  66. >                          //       char *infile, *outfile.  When
  67. >                          //       I try, I have not found a way to
  68. >                          //       dynamically allocate memory when the
  69. >                          //       user inputs the file.  I know I can
  70. >                          //       use something like infile = new char[X]
  71. >                          //       but that still leave me with the X limit.  
  72. >  cout << "Enter the path and filename of" << endl   
  73. >       << "the input file (*.csv): ";                
  74. >  cin  >> infile;         // I would like to allocate memory right after 
  75. >                          // user inputs the name of the file.  If I use
  76. >                          // *infile and leave it like this it crashes
  77. >                          // because of unallocated space
  78. >  InputFile.open(infile);       // Test to see if the input file opens
  79. >  if (!InputFile) {
  80. >     cerr << "Cannot open input file!" << endl;
  81. >     exit(1);
  82. >     }
  83. >  cout << "Enter the path and filename of" << endl  // get output file
  84. >       << "the output file (*.bcp): ";              // from user
  85. >  cin  >> outfile;
  86. >  OutputFile.open(outfile);    // Test to see if the output file opens
  87. >  if (!OutputFile) {
  88. >     cerr << "Cannot open output file!" << endl;
  89. >     exit(1);
  90. >     }
  91. > }
  92. -- 
  93. ----------------------------------------------------------------------
  94. Nitin More                                                         
  95. SunSoft, Bldg 16  Off: (415) 786 7109                                 
  96. Menlo Park, CA    Fax: (415) 786 7957   e-mail: nitin@more.eng.sun.com
  97. ----------------------------------------------------------------------
  98.